<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class Registration extends Controller {
    static protected $err_code = 0;
    // 签到接口
    static public function index_registration($param) {
        if (!isset($param['member_id'])) return ['data' => '', 'code' => 400001, 'msg' => 'FAULT'];

        try{
            $registration = Db::table('mob_registration')->where('member_id', $param['member_id'])->order('id desc')->find();
            $registration['point'] = Db::table('mob_member')->where('id', $param['member_id'])->value('point');
            return ['data'=>$registration,'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 签到接口
    static public function save_registration($param) {
        if(!isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        // 启动事务
        Db::startTrans();
        try{
            // 检查该用户是否已经签到，虽然前端会做判断，但是还是要以防万一
            $registration_time = strtotime(date('Y-m-d'));
            $check_registration =  self::check_registration($param);
            if ($check_registration['code'] != 200) {
                self::$err_code = $check_registration['code'];
                throw new \Exception($check_registration['msg']);
            }
            // 获取前一天签到记录
            $yesterday = Db::table('mob_registration')->where('add_time', $registration_time - 60 * 60 * 24)->where('member_id', $param['member_id'])->find();
            $days = $yesterday ? $yesterday['days'] + 1 : 1;
            if(date('w') == 1) {  // 每周一重置连续签到次数
                $days = 1;
            }
            
            // 按连续签到天数赠送摩豆  第一天送10个 第二天11个 第三天12个 第四天13个 第五天14个 第六天15个 第七天16个  每周一个循环
            $num = 10 + $days - 1;
            $tomorrow_num = 10 + $days;
            if ($days == 7) {
                $tomorrow_num = 10;
            }

            // 添加签到记录
            $data = [
                'member_id' => $param['member_id'],
                'add_time' => $registration_time,
                'days' => $days,
                'num' => $num
            ];
            Db::table('mob_registration')->insert($data);

            // 签到成功  赠送摩豆
            $integral_data = [
                'member_id' => $param['member_id'],
                'add_time' => time(),
                'num' => $num,
                'msg' => '签到赠送'
            ];
            Db::table('mob_integral')->insert($integral_data);

            // 更新用户表
            Db::table('mob_member')->where('id', $param['member_id'])->setInc('point', $num);

            // 提交事务
            Db::commit();
            return ['data'=>['days' => $days, 'num' => $num, 'tomorrow_num' => $tomorrow_num],'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 检查当天是否已经签到接口接口
    static public function check_registration($param) {
        if(!isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            // 检查该用户是否已经签到，虽然前端会做判断，但是还是要以防万一
            $registration_time = strtotime(date('Y-m-d'));
            $check_registration = Db::table('mob_registration')->where('add_time', $registration_time)->where('member_id', $param['member_id'])->find();
            if ($check_registration) {
                self::$err_code = 400075;
                throw new \Exception('FAULT');
            }
            return ['code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 获取签到明细列表
    static public function get_sign_in_list($param){
        if( !isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['member_id'] = $param['member_id'];
            $member_registration_list = Db::table('mob_registration')->where($where)->order('add_time desc')->paginate();
            return ['data'=>$member_registration_list, 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }
}
